﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
using umbraco.cms.businesslogic.web;
using umbraco;
using SPCFMedicineInformer.Helpers;
using System.Text;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class Admin_MedicineCategories : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
                BindDropdown();
            }
        }
        void BindGrid()
        {
            var list = NodeLocator.GetNodesOfExactType<MedicineCategory>();
            rptSymptomsListing.DataSource = list;
            rptSymptomsListing.DataBind();
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/utilities/medicine-categories/category-entry/");
        }
        void Delete(int id)
        {
            if (id > 0)
            {
                Document doc = new Document(id);
                if (doc.Published)
                {
                    doc.UnPublish();
                    library.UnPublishSingleNode(doc.Id);
                }
                doc.delete();
                BindGrid();
            }
        }
        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case "delete":
                        Delete(intTemp);
                        break;
                }
            }
        }
        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                MedicineCategory mc = (MedicineCategory)e.Item.DataItem;
                if (!string.IsNullOrEmpty(mc.Name))
                {
                    Literal litSubCategories = (Literal)e.Item.FindControl("litSubCategories");
                    Literal litGenName = (Literal)e.Item.FindControl("litGenNameCount");
                    Literal litMedicine = (Literal)e.Item.FindControl("litMedicineCount");
                    Literal litSymptoms = (Literal)e.Item.FindControl("litSymptomsCount");

                    litSubCategories.Text = FormHelper.GetChildrenTotal(mc.Id, "MedicineCategory");
                    litGenName.Text = FormHelper.GetChildrenTotal(mc.Id, "MedicineGenericName");
                    litMedicine.Text = FormHelper.GetDescendantsTotal(mc.Id, "MedicineItem");
                    litSymptoms.Text = FormHelper.GetDescendantsTotal(mc.Id, "Symptom");
                    
                    
                }
            }
        }
        void BindDropdown()
        {
            ddlTitles.Items.Add(new ListItem("Id", "Id"));
            ddlTitles.Items.Add(new ListItem("Category", "Category"));
        }
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            IEnumerable<MedicineCategory> list = NodeLocator.GetNodesOfExactType<MedicineCategory>().Where(a => a.Name.ToLower().Contains(txtSearch.Text));

            if (rdoSort.Text == "Asc")
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderBy(a => a.Id);
                        break;
                    case "Category":
                        list = list.OrderBy(a => a.Name);
                        break;
                }
            }
            else
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderByDescending(a => a.Id);
                        break;
                    case "Category":
                        list = list.OrderByDescending(a => a.Name);
                        break;
                }
            }



            rptSymptomsListing.DataSource = list;
            rptSymptomsListing.DataBind();

        }
    }
}